﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;
using System.Linq;
using System.Data.Odbc;
namespace Exact_Extension
{
    public partial class RevenueAllocateDetailForm : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true);
        ODBCIR odbcIR;
        public RevenueAllocateDetailForm(ExactDataContext _DS)
        {
            InitializeComponent();
            DS = _DS;
            odbcIR = new ODBCIR();
   
        }
        public List<ISHCMC_DebtorRegister> GetListStudent()
        {
            OdbcConnection conn = new OdbcConnection("DSN=ISIT-INRE;Uid=admin;Pwd=r3dGr@ss;");
            conn.Open();


            List<ISHCMC_DebtorRegister> ListStudent = new List<ISHCMC_DebtorRegister>();

            string sqlComm = " Select   idInquiry,NameFirst+' ',NameFirst_ovr+' ',NameMiddle+' ' "
                + ",NameLast+' ',P1a_First+' ',P1a_Last+' ' , P1a_phone_cell+' ',P1a_email+' ',P1b_First+' ',P1b_Last+' '  "
                + " , P1b_phone_cell+ ' ',P1b_email+' ' , P1_phone_H +' ' , P1_Street +' ' , P1_City +' ' , P1_Country +' ',PWSPaymentMethod +' ' "
                + " , Billing_Name + ' ',Adm_Season + ' ',Campus + ' ',Grade_Current+' ',Adm_enr_status + ' '  ,   idInquiryArc + ' '   from INQUIRIES  ";




            OdbcCommand comm = new OdbcCommand();
            comm.Connection = conn;
            comm.CommandText = sqlComm;

            OdbcDataReader reader = comm.ExecuteReader();
            int i = 0;
            while (reader.Read())
            {

                ISHCMC_DebtorRegister item = new ISHCMC_DebtorRegister();

                item.StudentIDIS = reader.GetString(0);

                item.StudentFirstName = reader.GetString(1);
                item.StudentFirstName += reader.GetString(2);

                item.StudentLastName = reader.GetString(3);
                item.StudentLastName += reader.GetString(4);

                item.FatherName = reader.GetString(5);
                item.FatherName += reader.GetString(6);
                item.FatherCellPhone = reader.GetString(7);
                item.FatherEmail = reader.GetString(8);

                item.MotherName = reader.GetString(9);
                item.MotherName += reader.GetString(10);

                item.MotherCellPhone = reader.GetString(11);
                item.MotherEmail = reader.GetString(12);

                item.Homephone = reader.GetString(13);
                item.Homeaddress1 = reader.GetString(14);
                item.Homeaddress2 = reader.GetString(15);
                item.Homeaddress3 = reader.GetString(16);

                item.PaymentPlan = reader.GetString(17);
                if (item.PaymentPlan.Length > 0)
                {
                    item.PaymentPlan = item.PaymentPlan.Substring(0, 1);
                }
                else
                {
                    item.PaymentPlan = "A";
                }
                item.BillingName = reader.GetString(18);

                item.BillingAddress1 = reader.GetString(14);
                item.BillingAddress2 = reader.GetString(15);
                item.BillingAddress3 = reader.GetString(16);

                item.ContactName1 = item.FatherName;
                item.ContactName2 = item.MotherName;


                item.Contactaddress1 = reader.GetString(14);
                item.Contactaddress2 = reader.GetString(15);
                item.Contactaddress3 = reader.GetString(16);

                item.SchoolYear = reader.GetString(19);
                item.School = reader.GetString(20);
                if (item.School == "A")
                {
                    item.School = "AAVN";
                }
                else if (item.School == "I")
                {
                    item.School = "ISHCMC";
                }
                item.Grade = reader.GetString(21);
                if (item.Grade.StartsWith("E"))
                {
                    item.Grade = item.Grade.Substring(1, 2);
                }
                if (item.Grade.Length == 1)
                {
                    item.Grade = "0" + item.Grade;
                }
                //item.ActualEntryDate = Convert.ToDateTime(reader.GetString(22));
                // item.DOB = reader.GetDate(23);
                //item.Gender =  reader.GetString(24);
                item.StudentStatus = reader.GetString(22);
                item.StudentIDExact = reader.GetString(23);
                if (item.StudentIDExact.Length > 4)
                {
                    item.StudentIDIS = item.StudentIDExact.Trim();
                }

                ListStudent.Add(item);

            }
            reader.Close();

            conn.Close();
            return ListStudent;

        }
        private void btnLoad_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            DateTime FromDate = Convert.ToDateTime(this.FromDateEdit.EditValue);
            DateTime ToDate = Convert.ToDateTime(this.ToDateEdit.EditValue);

            var query = from RevAll in DS.ISHCMC_F_RevenueAllocation(FromDate,ToDate)
                        select RevAll;
            var queryABBeforeDis = from RevAllBeforeDis in DS.ISHCMC_F_RevenueAllocationActualBillBeforeDis(FromDate,ToDate)
                                   select RevAllBeforeDis;
            var queryABAfterDis = from RevAllAfterDis in DS.ISHCMC_F_RevenueAllocationActualBillAfterDis(FromDate,ToDate)
                                  select RevAllAfterDis;
            List<ISHCMC_F_RevenueAllocationResult> ListRevAll = new List<ISHCMC_F_RevenueAllocationResult>();

            List<ISHCMC_F_RevenueAllocationResult> ListRevAll_Final = new List<ISHCMC_F_RevenueAllocationResult>();
            Hashtable hashDataST = new Hashtable();
            Hashtable hashInterest = new Hashtable();
            foreach (ISHCMC_DebtorRegister itemDR in this.GetListStudent())
            {
                hashDataST[itemDR.StudentIDIS] = itemDR.StudentStatus;
            }
            foreach (ISHCMC_F_RevenueAllocationActualBillAfterDisResult item in queryABAfterDis)
            {
                if (hashDataST.ContainsKey(item.StudentID))
                {
                    item.StudentStatus = hashDataST[item.StudentID].ToString();
                }
             
            }
            foreach (ISHCMC_F_RevenueAllocationActualBillAfterDisResult  item in queryABAfterDis)
            {
               
                if (hashDataST.ContainsKey(item.StudentID))
                {
                    item.StudentStatus = hashDataST[item.StudentID].ToString();
                }
                ISHCMC_F_RevenueAllocationResult newItem = new ISHCMC_F_RevenueAllocationResult();
                if (item.EnrollmentDate.HasValue)
                    newItem.EnrollmentDateStr = item.EnrollmentDate.Value.ToString("dd/MM/yyy");
                newItem.ProformaInvoiceNo = item.ProformaInvoiceNo;
                newItem.Debnr = item.Debnr;
                newItem.StudentID = item.StudentID;
                newItem.School = item.School;
                newItem.StudentName = item.StudentName;
                newItem.PaymentOption = item.PaymentOption;
                newItem.Artcode = item.Artcode;
                newItem.ReportingGroup = item.ReportingGroup;
                newItem.Grade = item.Grade;
                newItem.ActualBillAfterDisStr = item.ActualBillAfterDisStr;
                newItem.ActualBillBeforeDisStr = item.ActualBillBeforeDisStr;
                newItem.GLDebit = item.GLDebit;
                newItem.GLCredit = item.GLCredit;
                newItem.DiscountType = item.DiscountType;
                newItem.StudentStatus = item.StudentStatus;
                newItem.SchoolFee = item.SchoolFee;
                newItem.BudgetAmount = item.BudgetAmount;
                ListRevAll.Add(newItem);
            }

            foreach (ISHCMC_F_RevenueAllocationActualBillBeforeDisResult item in queryABBeforeDis)
            {
               
                if (hashDataST.ContainsKey(item.StudentID))
                {
                    item.StudentStatus = hashDataST[item.StudentID].ToString();
                }
                 ISHCMC_F_RevenueAllocationResult newItem = new  ISHCMC_F_RevenueAllocationResult();
                if (item.EnrollmentDate.HasValue)
                    newItem.EnrollmentDateStr = item.EnrollmentDate.Value.ToString("dd/MM/yyy");
                newItem.ProformaInvoiceNo = item.ProformaInvoiceNo;
                newItem.Debnr = item.Debnr;
                newItem.StudentID = item.StudentID;
                newItem.School = item.School;
                newItem.StudentName = item.StudentName;
                newItem.PaymentOption = item.PaymentOption;
                newItem.Artcode = item.Artcode;
                newItem.ReportingGroup = item.ReportingGroup;
                newItem.Grade = item.Grade;
                newItem.ActualBillAfterDisStr = item.ActualBillAfterDisStr;
                newItem.ActualBillBeforeDisStr = item.ActualBillBeforeDisStr;
                newItem.GLDebit = item.GLDebit;
                newItem.GLCredit = item.GLCredit;
                newItem.DiscountType = item.DiscountType;
                newItem.StudentStatus = item.StudentStatus;
                newItem.SchoolFee = item.SchoolFee;
                newItem.BudgetAmount = item.BudgetAmount;
                ListRevAll.Add(newItem);
            }
            foreach ( ISHCMC_F_RevenueAllocationResult item in query)
            {
                if(item.EnrollmentDate.HasValue)
                item.EnrollmentDateStr = item.EnrollmentDate.Value.ToString("dd/MM/yyy");
                if (item.ActualAmountAfterDiscount.HasValue)
                    item.ActualBillAfterDisStr = item.ActualAmountAfterDiscount.Value.ToString("#,#");
                if (item.ActualAmountBeforeDiscount.HasValue)
                    item.ActualBillBeforeDisStr = item.ActualAmountBeforeDiscount.Value.ToString("#,#");

                if (item.ReportingGroup == "Discount")
                {
                    item.DiscountType = item.GLDebit;
                }
                 if (hashDataST.ContainsKey(item.StudentID))
                {
                item.StudentStatus = hashDataST[item.StudentID].ToString();
                }
                 if (item.StudentStatus.Trim() == "Withdrawn" || item.StudentStatus.Trim() == "Closed")
                 {
                     if (item.Artcode == "TF" || item.Artcode == "APPF" || item.Artcode == "ADF" )
                     {
                         var query12 = from SCHY12 in DS.ISHCMC_SchoolYearAllocates
                                       where SCHY12.SEQ >= 1 && SCHY12.SEQ <= 12
                                       select SCHY12;
                         Hashtable hashData = new Hashtable();
                         foreach (ISHCMC_SchoolYearAllocate itemSCHY in query12)
                         {
                              ISHCMC_F_RevenueAllocationResult newItem = new  ISHCMC_F_RevenueAllocationResult();
                             newItem.ProformaInvoiceNo = item.ProformaInvoiceNo;
                             newItem.Debnr = item.Debnr;
                             newItem.StudentID = item.StudentID;
                             newItem.School = item.School;
                             newItem.StudentName = item.StudentName;
                             newItem.PaymentOption = item.PaymentOption;
                             newItem.Artcode = item.Artcode;
                             newItem.ReportingGroup = item.ReportingGroup;
                             newItem.Grade = item.Grade;
                             newItem.ActualBillAfterDisStr = item.ActualBillAfterDisStr;
                             newItem.ActualBillBeforeDisStr = item.ActualBillBeforeDisStr;
                             newItem.EnrollmentDateStr = item.EnrollmentDateStr;
                             newItem.DiscountType = item.DiscountType;
                             newItem.StudentStatus = item.StudentStatus;
                             newItem.SchoolFee = item.SchoolFee;
                             if (itemSCHY.SEQ.Value == 1)
                             {
                                 newItem.BudgetAmount = item.ActualAmountBeforeDiscount.Value;
                             }
                             else
                             {
                                 newItem.BudgetAmount = 0;
                             }
                             newItem.GLDebit = item.GLDebit;
                             newItem.GLCredit = item.GLCredit;
                             newItem.Period = itemSCHY.MonthDes;
                             hashData[itemSCHY.SEQ] = newItem;
                             ListRevAll.Add(newItem);
                         }
                     }

                 }
                 else
                 {
                     if (item.PaymentOption == "A")
                     {
                         var query12 = from SCHY12 in DS.ISHCMC_SchoolYearAllocates
                                       where SCHY12.SEQ >= 1 && SCHY12.SEQ <= 12
                                       select SCHY12;
                         Hashtable hashData = new Hashtable();
                         foreach (ISHCMC_SchoolYearAllocate itemSCHY in query12)
                         {
                              ISHCMC_F_RevenueAllocationResult newItem = new  ISHCMC_F_RevenueAllocationResult();
                             newItem.ProformaInvoiceNo = item.ProformaInvoiceNo;
                             newItem.Debnr = item.Debnr;
                             newItem.StudentID = item.StudentID;
                             newItem.School = item.School;
                             newItem.StudentName = item.StudentName;
                             newItem.PaymentOption = item.PaymentOption;
                             newItem.Artcode = item.Artcode;
                             newItem.ReportingGroup = item.ReportingGroup;
                             newItem.Grade = item.Grade;
                             newItem.ActualBillAfterDisStr = item.ActualBillAfterDisStr;
                             newItem.ActualBillBeforeDisStr = item.ActualBillBeforeDisStr;
                             newItem.EnrollmentDateStr = item.EnrollmentDateStr;
                             newItem.DiscountType = item.DiscountType;
                             newItem.StudentStatus = item.StudentStatus;
                             newItem.SchoolFee = item.SchoolFee;
                             newItem.BudgetAmount = Math.Round(item.BudgetAmount.Value / 12);
                             newItem.Period = itemSCHY.MonthDes;
                             newItem.GLDebit = item.GLDebit;
                             newItem.GLCredit = item.GLCredit;
                             hashData[itemSCHY.SEQ] = newItem;

                         }
                         if (item.Artcode == "GTF")
                         {
                             double recheck = 0;

                             foreach (object key in hashData.Keys)
                             {
                                  ISHCMC_F_RevenueAllocationResult itemAllocated = ( ISHCMC_F_RevenueAllocationResult)hashData[key];
                                 recheck += itemAllocated.BudgetAmount.Value;



                             }
                             foreach (object key in hashData.Keys)
                             {
                                  ISHCMC_F_RevenueAllocationResult itemAllocated = ( ISHCMC_F_RevenueAllocationResult)hashData[key];


                                 if (key.ToString() == "6")
                                 {
                                     if (recheck > item.BudgetAmount.Value)
                                         itemAllocated.BudgetAmount = itemAllocated.BudgetAmount - (recheck - (item.BudgetAmount.Value / 12) * 12);
                                     if (recheck < item.BudgetAmount.Value)
                                         itemAllocated.BudgetAmount = itemAllocated.BudgetAmount + ((item.BudgetAmount.Value / 12) * 12 - recheck);
                                 }

                                 ListRevAll.Add(itemAllocated);
                             }
                         }
                         else
                         {
                             double recheck = 0;
                             foreach (object key in hashData.Keys)
                             {
                                  ISHCMC_F_RevenueAllocationResult itemAllocated = ( ISHCMC_F_RevenueAllocationResult)hashData[key];
                                 recheck += itemAllocated.BudgetAmount.Value;



                             }
                             foreach (object key in hashData.Keys)
                             {
                                  ISHCMC_F_RevenueAllocationResult itemAllocated = ( ISHCMC_F_RevenueAllocationResult)hashData[key];


                                 if (key.ToString() == "12")
                                 {
                                     if (recheck > item.BudgetAmount.Value)
                                         itemAllocated.BudgetAmount = itemAllocated.BudgetAmount - (recheck - (item.BudgetAmount.Value / 12) * 12);
                                     if (recheck < item.BudgetAmount.Value)
                                         itemAllocated.BudgetAmount = itemAllocated.BudgetAmount + ((item.BudgetAmount.Value / 12) * 12 - recheck);
                                 }

                                 ListRevAll.Add(itemAllocated);
                             }
                         }

                     }
                     if (item.PaymentOption == "S")
                     {
                         if (item.Artcode == "TF" || item.Artcode == "FT" || item.Artcode == "ITF" || item.Artcode == "GTF")
                         {
                             var query12 = from SCHY12 in DS.ISHCMC_SchoolYearAllocates
                                           where SCHY12.SEQ >= 1 && SCHY12.SEQ <= 6
                                           select SCHY12;
                             Hashtable hashData = new Hashtable();
                             foreach (ISHCMC_SchoolYearAllocate itemSCHY in query12)
                             {
                                  ISHCMC_F_RevenueAllocationResult newItem = new  ISHCMC_F_RevenueAllocationResult();
                                 newItem.ProformaInvoiceNo = item.ProformaInvoiceNo;
                                 newItem.Debnr = item.Debnr;
                                 newItem.StudentID = item.StudentID;
                                 newItem.School = item.School;
                                 newItem.StudentName = item.StudentName;
                                 newItem.PaymentOption = item.PaymentOption;
                                 newItem.Artcode = item.Artcode;
                                 newItem.ReportingGroup = item.ReportingGroup;
                                 newItem.Grade = item.Grade;
                                 newItem.ActualBillAfterDisStr = item.ActualBillAfterDisStr;
                                 newItem.ActualBillBeforeDisStr = item.ActualBillBeforeDisStr;
                                 newItem.EnrollmentDateStr = item.EnrollmentDateStr;
                                 newItem.DiscountType = item.DiscountType;
                                 newItem.StudentStatus = item.StudentStatus;
                                 newItem.SchoolFee = item.SchoolFee;
                                 newItem.BudgetAmount = Math.Round(item.BudgetAmount.Value / 12);
                                 newItem.Period = itemSCHY.MonthDes;
                                 newItem.GLDebit = item.GLDebit;
                                 newItem.GLCredit = item.GLCredit;
                                 hashData[itemSCHY.SEQ] = newItem;

                             }
                             if (item.Artcode == "GTF")
                             {
                                 double recheck = 0;

                                 foreach (object key in hashData.Keys)
                                 {
                                      ISHCMC_F_RevenueAllocationResult itemAllocated = ( ISHCMC_F_RevenueAllocationResult)hashData[key];
                                     recheck += itemAllocated.BudgetAmount.Value;



                                 }
                                 foreach (object key in hashData.Keys)
                                 {
                                      ISHCMC_F_RevenueAllocationResult itemAllocated = ( ISHCMC_F_RevenueAllocationResult)hashData[key];


                                     if (key.ToString() == "6")
                                     {
                                         if (recheck > item.BudgetAmount.Value)
                                             itemAllocated.BudgetAmount = itemAllocated.BudgetAmount - (recheck - (item.BudgetAmount.Value / 12) * 6);
                                         if (recheck < item.BudgetAmount.Value)
                                             itemAllocated.BudgetAmount = itemAllocated.BudgetAmount + ((item.BudgetAmount.Value / 12) * 6 - recheck);
                                     }

                                     ListRevAll.Add(itemAllocated);
                                 }
                             }
                             else
                             {
                                 double recheck = 0;
                                 foreach (object key in hashData.Keys)
                                 {
                                     ISHCMC_F_RevenueAllocationResult itemAllocated = ( ISHCMC_F_RevenueAllocationResult)hashData[key];
                                     recheck += itemAllocated.BudgetAmount.Value;



                                 }
                                 foreach (object key in hashData.Keys)
                                 {
                                      ISHCMC_F_RevenueAllocationResult itemAllocated = ( ISHCMC_F_RevenueAllocationResult)hashData[key];


                                     if (key.ToString() == "6")
                                     {
                                         if (recheck > item.BudgetAmount.Value)
                                             itemAllocated.BudgetAmount = itemAllocated.BudgetAmount - (recheck - (item.BudgetAmount.Value / 12) * 6);
                                         if (recheck < item.BudgetAmount.Value)
                                             itemAllocated.BudgetAmount = itemAllocated.BudgetAmount + ((item.BudgetAmount.Value / 12) * 6 - recheck);
                                     }

                                     ListRevAll.Add(itemAllocated);
                                 }
                             }

                         }
                         else
                         {
                             var query12 = from SCHY12 in DS.ISHCMC_SchoolYearAllocates
                                           where SCHY12.SEQ >= 1 && SCHY12.SEQ <= 12
                                           select SCHY12;
                             Hashtable hashData = new Hashtable();
                             foreach (ISHCMC_SchoolYearAllocate itemSCHY in query12)
                             {
                                  ISHCMC_F_RevenueAllocationResult newItem = new  ISHCMC_F_RevenueAllocationResult();
                                 newItem.ProformaInvoiceNo = item.ProformaInvoiceNo;
                                 newItem.Debnr = item.Debnr;
                                 newItem.StudentID = item.StudentID;
                                 newItem.School = item.School;
                                 newItem.StudentName = item.StudentName;
                                 newItem.PaymentOption = item.PaymentOption;
                                 newItem.Artcode = item.Artcode;
                                 newItem.ReportingGroup = item.ReportingGroup;
                                 newItem.Grade = item.Grade;
                                 newItem.ActualBillAfterDisStr = item.ActualBillAfterDisStr;
                                 newItem.ActualBillBeforeDisStr = item.ActualBillBeforeDisStr;
                                 newItem.EnrollmentDateStr = item.EnrollmentDateStr;
                                 newItem.DiscountType = item.DiscountType;
                                 newItem.StudentStatus = item.StudentStatus;
                                 newItem.SchoolFee = item.SchoolFee;
                                 newItem.BudgetAmount = Math.Round(item.BudgetAmount.Value / 12);
                                 newItem.Period = itemSCHY.MonthDes;
                                 newItem.GLDebit = item.GLDebit;
                                 newItem.GLCredit = item.GLCredit;
                                 hashData[itemSCHY.SEQ] = newItem;

                             }

                             double recheck = 0;
                             foreach (object key in hashData.Keys)
                             {
                                  ISHCMC_F_RevenueAllocationResult itemAllocated = ( ISHCMC_F_RevenueAllocationResult)hashData[key];
                                 recheck += itemAllocated.BudgetAmount.Value;



                             }
                             foreach (object key in hashData.Keys)
                             {
                                  ISHCMC_F_RevenueAllocationResult itemAllocated = ( ISHCMC_F_RevenueAllocationResult)hashData[key];


                                 if (key.ToString() == "12")
                                 {
                                     if (recheck > item.BudgetAmount.Value)
                                         itemAllocated.BudgetAmount = itemAllocated.BudgetAmount - (recheck - (item.BudgetAmount.Value / 12) * 12);
                                     if (recheck < item.BudgetAmount.Value)
                                         itemAllocated.BudgetAmount = itemAllocated.BudgetAmount + ((item.BudgetAmount.Value / 12) * 12 - recheck);
                                 }

                                 ListRevAll.Add(itemAllocated);
                             }

                         }
                     }
                     if (item.PaymentOption == "Q")
                     {
                         if (item.Artcode == "TF" || item.Artcode == "FT" || item.Artcode == "ITF" || item.Artcode == "GTF")
                         {
                             var query12 = from SCHY12 in DS.ISHCMC_SchoolYearAllocates
                                           where SCHY12.SEQ >= 1 && SCHY12.SEQ <= 3
                                           select SCHY12;
                             Hashtable hashData = new Hashtable();
                             foreach (ISHCMC_SchoolYearAllocate itemSCHY in query12)
                             {
                                  ISHCMC_F_RevenueAllocationResult newItem = new  ISHCMC_F_RevenueAllocationResult();
                                 newItem.ProformaInvoiceNo = item.ProformaInvoiceNo;
                                 newItem.Debnr = item.Debnr;
                                 newItem.StudentID = item.StudentID;
                                 newItem.School = item.School;
                                 newItem.StudentName = item.StudentName;
                                 newItem.PaymentOption = item.PaymentOption;
                                 newItem.Artcode = item.Artcode;
                                 newItem.ReportingGroup = item.ReportingGroup;
                                 newItem.Grade = item.Grade;
                                 newItem.ActualBillAfterDisStr = item.ActualBillAfterDisStr;
                                 newItem.ActualBillBeforeDisStr = item.ActualBillBeforeDisStr;
                                 newItem.EnrollmentDateStr = item.EnrollmentDateStr;
                                 newItem.DiscountType = item.DiscountType;
                                 newItem.StudentStatus = item.StudentStatus;
                                 newItem.SchoolFee = item.SchoolFee;
                                 newItem.BudgetAmount = Math.Round(item.BudgetAmount.Value / 12);
                                 newItem.Period = itemSCHY.MonthDes;
                                 newItem.GLDebit = item.GLDebit;
                                 newItem.GLCredit = item.GLCredit;
                                 hashData[itemSCHY.SEQ] = newItem;

                             }
                             if (item.Artcode == "GTF")
                             {
                                 double recheck = 0;

                                 foreach (object key in hashData.Keys)
                                 {
                                     ISHCMC_F_RevenueAllocationResult  itemAllocated = (ISHCMC_F_RevenueAllocationResult )hashData[key];
                                     recheck += itemAllocated.BudgetAmount.Value;



                                 }
                                 foreach (object key in hashData.Keys)
                                 {
                                     ISHCMC_F_RevenueAllocationResult itemAllocated = (ISHCMC_F_RevenueAllocationResult )hashData[key];


                                     if (key.ToString() == "3")
                                     {
                                         if (recheck > item.BudgetAmount.Value)
                                             itemAllocated.BudgetAmount = itemAllocated.BudgetAmount - (recheck - (item.BudgetAmount.Value / 12) * 3);
                                         if (recheck < item.BudgetAmount.Value)
                                             itemAllocated.BudgetAmount = itemAllocated.BudgetAmount + ((item.BudgetAmount.Value / 12) * 3 - recheck);
                                     }

                                     ListRevAll.Add(itemAllocated);
                                 }
                             }
                             else
                             {
                                 double recheck = 0;

                                 foreach (object key in hashData.Keys)
                                 {
                                     ISHCMC_F_RevenueAllocationResult itemAllocated = (ISHCMC_F_RevenueAllocationResult )hashData[key];
                                     recheck += itemAllocated.BudgetAmount.Value;



                                 }
                                 foreach (object key in hashData.Keys)
                                 {
                                     ISHCMC_F_RevenueAllocationResult itemAllocated = (ISHCMC_F_RevenueAllocationResult )hashData[key];


                                     if (key.ToString() == "3")
                                     {
                                         if (recheck > item.BudgetAmount.Value)
                                             itemAllocated.BudgetAmount = itemAllocated.BudgetAmount - (recheck - (item.BudgetAmount.Value / 12) * 3);
                                         if (recheck < item.BudgetAmount.Value)
                                             itemAllocated.BudgetAmount = itemAllocated.BudgetAmount + ((item.BudgetAmount.Value / 12) * 3 - recheck);
                                     }

                                     ListRevAll.Add(itemAllocated);
                                 }
                             }

                         }
                         else
                         {
                             var query12 = from SCHY12 in DS.ISHCMC_SchoolYearAllocates
                                           where SCHY12.SEQ >= 1 && SCHY12.SEQ <= 12
                                           select SCHY12;
                             Hashtable hashData = new Hashtable();
                             foreach (ISHCMC_SchoolYearAllocate itemSCHY in query12)
                             {
                                 ISHCMC_F_RevenueAllocationResult newItem = new ISHCMC_F_RevenueAllocationResult ();
                                 newItem.ProformaInvoiceNo = item.ProformaInvoiceNo;
                                 newItem.Debnr = item.Debnr;
                                 newItem.StudentID = item.StudentID;
                                 newItem.School = item.School;
                                 newItem.StudentName = item.StudentName;
                                 newItem.PaymentOption = item.PaymentOption;
                                 newItem.Artcode = item.Artcode;
                                 newItem.ReportingGroup = item.ReportingGroup;
                                 newItem.Grade = item.Grade;
                                 newItem.ActualBillAfterDisStr = item.ActualBillAfterDisStr;
                                 newItem.ActualBillBeforeDisStr = item.ActualBillBeforeDisStr;
                                 newItem.EnrollmentDateStr = item.EnrollmentDateStr;
                                 newItem.DiscountType = item.DiscountType;
                                 newItem.StudentStatus = item.StudentStatus;
                                 newItem.SchoolFee = item.SchoolFee;
                                 newItem.BudgetAmount = Math.Round(item.BudgetAmount.Value / 12);
                                 newItem.Period = itemSCHY.MonthDes;
                                 newItem.GLDebit = item.GLDebit;
                                 newItem.GLCredit = item.GLCredit;
                                 hashData[itemSCHY.SEQ] = newItem;

                             }


                             double recheck = 0;
                             foreach (object key in hashData.Keys)
                             {
                                 ISHCMC_F_RevenueAllocationResult itemAllocated = (ISHCMC_F_RevenueAllocationResult )hashData[key];
                                 recheck += itemAllocated.BudgetAmount.Value;



                             }
                             foreach (object key in hashData.Keys)
                             {
                                 ISHCMC_F_RevenueAllocationResult itemAllocated = (ISHCMC_F_RevenueAllocationResult )hashData[key];


                                 if (key.ToString() == "12")
                                 {
                                     if (recheck > item.BudgetAmount.Value)
                                         itemAllocated.BudgetAmount = itemAllocated.BudgetAmount - (recheck - (item.BudgetAmount.Value / 12) * 12);
                                     if (recheck < item.BudgetAmount.Value)
                                         itemAllocated.BudgetAmount = itemAllocated.BudgetAmount + ((item.BudgetAmount.Value / 12) * 12 - recheck);
                                 }

                                 ListRevAll.Add(itemAllocated);
                             }
                         }
                     }
                 




                 }
                 
            }
           
            this.iSHCMCVRevenueAllocationBindingSource.DataSource = ListRevAll;
        }
        private void btnExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gridControl1.ShowRibbonPrintPreview();
        }

    }
}